import { DeleteGlobalMisson, MountGlobalMisson } from '@/module/globalMisson/work'
import {MissonModel} from '@/mount/room/Misson/missonModel'
import { colorful, isInArray } from '@/utils'
import { LabMap } from '@/module/labMap'
import room from '@/mount/room'
/* 行动相关任务集合 */
export default {
    /* 斥候 */
    scout:{
        /* 签名 */
        sign(myRoom:string,disRoom:string,words:string):string{
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[partrol]未找到房间`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['房间签名'])
            thisTask.relateCreep = ['scout']
            thisTask.disRoom = [disRoom]
            thisTask.words = words
            thisTask.bindData = {'scout':{num:1,bind:[]}}
            if(thisRoom.mountMisson(thisTask))
            return `[partrol]房间${myRoom}挂载sign任务成功!`
            else
            return `[partrol]房间${myRoom}挂载sign任务失败!`
        },
        Csign(myRoom:string,ID:string){
            var thisRoom = Game.rooms[myRoom]
            if (!thisRoom) return `[partrol]未找到房间!`
            if (thisRoom.deleteMisson_ID(ID))
            return `[partrol]房间${myRoom}删除sign任务成功!`
            else return `[partrol]房间${myRoom}不存在sign任务!`
        }
    },

    /* 可视化 */
    visual:{
        Tower(shard:number ,disRoom:string,sec:number,type:'attack'|'repair'|'heal'):string{
            if (!global.towerRangeData) global.towerRangeData = {}
            if (!global.towerRangeData[disRoom])
            {
                if (!Game.rooms[disRoom])
                    return `[visual]目前不存在房间${disRoom}的视野！`
                else
                {
                    Game.rooms[disRoom].TowerRangeMap()
                }
            }
            if (!global.towerRangeData[disRoom]) return `[visual] 防御塔数据可视化任务未知错误!`
            var thisTask:GlobalMissonData = {name:'防御塔数据可视化',delayTick:sec,disroom:[disRoom],ID:'',shard:shard,onlyOnce:true,type:type}
            if(MountGlobalMisson(thisTask))
                return `[visual]成功添加<防御塔数据可视化>任务!`
            else
                return `[visual]添加<防御塔数据可视化任务失败!>`
        },
        CTower():string{
            if(DeleteGlobalMisson('防御塔数据可视化'))
                return `[visual]成功删除<防御塔数据可视化>任务!`
            else
                return `[visual]删除<防御塔数据可视化>任务失败!`
        }
    },

    /* 核弹相关 */
    nuke:{
        /* 发射核弹 */
        launch(roomName:string,disRoom:string,x_:number,y_:number):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[nuke]房间错误，请确认房间${roomName}！`
            var nuke_ = Game.getObjectById(myRoom.memory.StructureIdData.NukerID as string) as StructureNuker
            if (!nuke_) return `[nuke]核弹查询错误!`
            if (nuke_.launchNuke(new RoomPosition(x_,y_,disRoom)) == OK)
                return colorful(`[nuke]${roomName}->${disRoom}的核弹发射成功!预计---500000---ticks后着陆!`,'red',true)
            else
                return colorful(`[nuke]${roomName}->${disRoom}的核弹发射失败!`,'yellow',true)
        }
    },

    /* 升级相关 */
    upgrade:{
        rush(roomName:string,num:number):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[upgrade]房间错误，请确认房间${roomName}！`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['控制冲级'])
            thisTask.numData = {}
            thisTask.numData['i-upgrader'] = num
            thisTask.relateCreep = ['i-upgrader']
            thisTask.switch = false
            
            if (myRoom.mountMisson(thisTask)) return colorful(`[upgrade]房间${roomName}挂载冲级任务成功,任务爬虫数${num}个!`,'blue')
            return colorful(`[upgrade]房间${roomName}挂载冲级任务失败!`,'red',false)
        },
        Crush(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[upgrade]房间错误，请确认房间${roomName}！`
            if (myRoom.deleteMisson('CREEP','控制冲级'))
                return colorful(`[upgrade]房间${roomName}删除冲级任务成功!`,'blue',false)
            return colorful(`[upgrade]房间${roomName}删除冲级任务失败!`,'red',false)
        },
        Brush(roomName:string,index:number,rType:'GH'|'GH2O'|'XGH2O' = 'GH',creepNum:number =1,num:number = 2):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[upgrade]房间错误，请确认房间${roomName}！`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['控制冲级'])
            thisTask.numData = {}
            thisTask.numData['i-upgrader'] = creepNum
            thisTask.relateCreep = ['i-upgrader']
            if (!thisTask) return `[upgrade]房间${roomName}未找到控制冲级任务!`
            thisTask.switch = true
            thisTask.relateResource = [rType]
            var relateFlagName = `${roomName}/${index}`
            if (!Game.flags[relateFlagName]) return `[upgrade]未找到旗帜${relateFlagName}!`
            /* 获取lab */
            var disLab = Game.flags[relateFlagName].pos.GetStructure('lab')
            if (!disLab) return `[upgrade]旗帜${relateFlagName}下未获取到lab建筑!`
            /* 实验室、旗帜绑定数据 */
            thisTask.LabBind = {}
            /* 旗帜名为key 参数为列表，第一个参数是相关lab的id 第二个参数是该lab相关的资源类型 第三个参数是允许的最大数量 */
            thisTask.LabBind[relateFlagName] = [disLab.id,thisTask.relateResource[0],num]
            if(myRoom.mountMisson(thisTask))
            return `[upgrade]boost冲级任务挂载成功!`
            else
            return `[upgrade]boost冲级任务挂载失败!`
        },
    },

    /* 修墙相关 */
    repair:{
        /* 全局刷墙 房间名 刷墙工数量 目标hit */
        global(roomName:string,num:number = 1,disNum:number = 100000000):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[repair]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '爬虫刷墙' && i.type && i.type == '全局刷墙')
                return colorful(`[repair] 房间${roomName}已经存在一个全局刷墙任务了，不能再添加了!`,'yellow')
            }
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['爬虫刷墙'])
            thisTask.bindData = {'repair01':{num:num,bind:[]}}
            thisTask.disnum = disNum
            thisTask.relateCreep = ['repair01']
            thisTask.type = '全局刷墙'
            thisTask.switch = false
            if(myRoom.mountMisson(thisTask))
            return colorful(`[repair]${roomName}挂载全局刷墙任务成功!`,'green',true)
            else
            return colorful(`[repair]${roomName}挂载全局刷墙任务失败!`,'red',true)
        },
        Cglobal(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[repair]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '爬虫刷墙' && i.type && i.type == '全局刷墙')
                {
                    if(myRoom.deleteMisson_ID(i.ID)) return colorful(`[repair]房间${roomName}的全局刷墙任务已经删除!`,'green')
                }
            }
            return `[repair] 房间${roomName}未找到全局刷墙任务!`
        },
        /* 针对刷墙 房间名 刷墙工数量 目标hit 需要黄白旗 */
        special(roomName:string,num:number = 1,disNum:number = 100000000):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[repair]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '爬虫刷墙' && i.type && i.type == '针对刷墙')
                return colorful(`[repair] 房间${roomName}已经存在一个针对刷墙任务了，不能再添加了!`,'yellow')
            }
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['爬虫刷墙'])
            thisTask.bindData = {'repair01':{num:num,bind:[]}}
            thisTask.disnum = disNum
            thisTask.relateCreep = ['repair01']
            thisTask.type = '针对刷墙'
            thisTask.switch = false
            if(myRoom.mountMisson(thisTask))
            return colorful(`[repair]${roomName}挂载针对刷墙任务成功!`,'green',true)
            else
            return colorful(`[repair]${roomName}挂载针对刷墙任务失败!`,'red',true)
        },
        Cspecial(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[repair]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '爬虫刷墙' && i.type && i.type == '针对刷墙')
                {
                    if(myRoom.deleteMisson_ID(i.ID)) return colorful(`[repair]房间${roomName}的针对刷墙任务已经删除!`,'green')
                }
            }
            return `[repair] 房间${roomName}未找到针对刷墙任务!`
        },
        /* 刷墙工boost选项 */
        Bglobal(roomName:string,num:number,rType:ResourceConstant = 'XLH2O'):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[repair]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '强化刷墙' && i.type && i.type == '全局刷墙')
                return colorful(`[repair] 房间${roomName}已经存在一个强化刷墙任务了，不能再添加了!`,'yellow')
            }
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['强化刷墙'])
            thisTask.bindData = {'Brepair':{num:num,bind:[]}}
            thisTask.disnum = 300000000
            thisTask.relateCreep = ['Brepair']
            thisTask.type = '全局刷墙'
            thisTask.switch = false
            // 实验室筛选
            var labs = myRoom.memory.TaskMemory.labs
            if (!labs || labs.length < 1) return `[repair]实验室数量不足，强化刷墙任务取消!`
            var NuN = []
            var NuR = [rType]
            var Data = {}
            for (var l of labs)
            {
                if (!myRoom.memory.TaskData.OccupiedLab[l] && NuN.length < 1)
                {
                    NuN.push(l)
                }
            }
            if (NuN.length < 1) return `[repair]空闲实验室数量不足，强化刷墙任务取消!`
            for (var n in NuN)
            {
                var thisLab = Game.getObjectById(NuN[n]) as StructureLab
                if (!thisLab) return `[repair]不存在lab，未知错误!`
                var thisFlag = thisLab.pos.lookFor(LOOK_FLAGS)[0]
                if (!thisFlag) return `[repair]lab${thisLab.id}上不存在旗帜!`
                Data[thisFlag.name] = [thisLab.id,NuR[n],1]
            }
            thisTask.LabBind = Data
            // 挂载任务
            if(myRoom.mountMisson(thisTask))
            return colorful(`[repair]${roomName}挂载强化刷墙任务成功!`,'green',true)
            else
            return colorful(`[repair]${roomName}挂载强化刷墙任务失败!`,'red',true)
        },
        /* 刷墙工unboost选项 */
        CBglobal(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[repair]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '强化刷墙' && i.type && i.type == '全局刷墙')
                {
                    if(myRoom.deleteMisson_ID(i.ID)) return colorful(`[repair]房间${roomName}的全局刷墙任务已经删除!`,'green',true)
                }
            }
            return `[repair] 房间${roomName}未找到全局刷墙任务!`
        },
        /* 针对刷墙 */
        Bspecial(roomName:string,num:number,rType:ResourceConstant = 'XLH2O'):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[repair]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '强化刷墙' && i.type && i.type == '针对刷墙')
                return colorful(`[repair] 房间${roomName}已经存在一个强化刷墙任务了，不能再添加了!`,'yellow')
            }
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['强化刷墙'])
            thisTask.bindData = {'Brepair':{num:num,bind:[]}}
            thisTask.disnum = 300000000
            thisTask.relateCreep = ['Brepair']
            thisTask.type = '针对刷墙'
            thisTask.switch = false
            // 实验室筛选
            var labs = myRoom.memory.TaskMemory.labs
            if (!labs || labs.length < 1) return `[repair]实验室数量不足，强化刷墙任务取消!`
            var NuN = []
            var NuR = [rType]
            var Data = {}
            for (var l of labs)
            {
                if (!myRoom.memory.TaskData.OccupiedLab[l] && NuN.length < 1)
                {
                    NuN.push(l)
                }
            }
            if (NuN.length < 1) return `[repair]空闲实验室数量不足，针对刷墙任务取消!`
            for (var n in NuN)
            {
                var thisLab = Game.getObjectById(NuN[n]) as StructureLab
                if (!thisLab) return `[repair]不存在lab，未知错误!`
                var thisFlag = thisLab.pos.lookFor(LOOK_FLAGS)[0]
                if (!thisFlag) return `[repair]lab${thisLab.id}上不存在旗帜!`
                Data[thisFlag.name] = [thisLab.id,NuR[n],1]
            }
            thisTask.LabBind = Data
            // 挂载任务
            if(myRoom.mountMisson(thisTask))
            return colorful(`[repair]${roomName}挂载强化针对刷墙任务成功!`,'green',true)
            else
            return colorful(`[repair]${roomName}挂载强化针对刷墙任务失败!`,'red',true)
        },
        CBspecial(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[repair]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP'])
            {
                if (i.Name == '强化刷墙' && i.type && i.type == '针对刷墙')
                {
                    if(myRoom.deleteMisson_ID(i.ID)) return colorful(`[repair]房间${roomName}的针对刷墙任务已经删除!`,'green',true)
                }
            }
            return `[repair] 房间${roomName}未找到针对刷墙任务!`
        }
    },

    /* 援建相关 */
    build:{
        /* 领土扩张型 援建 */
        expand(roomName:string,disRoom:string,num:number = 1):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[build]房间错误，请确认房间${roomName}！`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['扩张援建'])
            thisTask.bindData = {'e-claimer':{num:1,bind:[]},
                                'e-upgrader':{num:num,bind:[]},
                                'e-builder':{num:num,bind:[]},
                                }
            thisTask.disRoom = [disRoom]
            thisTask.relateCreep = ['e-claimer','e-upgrader','e-builder']
            thisTask.switch = false
            if(myRoom.mountMisson(thisTask))
            return colorful(`[build]${roomName}挂载扩张援建任务成功!`,'green',true)
            else
            return colorful(`[build]${roomName}挂载扩张援建任务失败!`,'red',true)
        },
        Cexpand(roomName:string,disRoom:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[build]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP']){
                if (i.Name == '扩张援建' && i.disRoom[0] == disRoom)
                    myRoom.deleteMisson_ID(i.ID)
            }
            return colorful(`[build]${roomName}->${disRoom}扩张援建任务 删除成功!`,'green',true)
        },
        /* 被推抢修型 援建 */
        urgent(roomName:string,disRoom:string):string{
            return
        },
    },

    /* 运输相关 */
    carry:{
        /* 掠夺任务 */
        pillage(roomName:string,disRoom:string,num:number):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[carry]房间错误，请确认房间${roomName}!`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['资源掠夺'])
            var startPos = new RoomPosition(myRoom.memory.center_x,myRoom.memory.center_y,roomName)
            //var pathCost = startPos.GetPath(new RoomPosition(25,25,disRoom),60).cost
            //console.log(`[carry]预计往返所需tick为${pathCost*2+10}`)
            thisTask.num = 800//pathCost*2 + 10
            thisTask.relateCreep = ['pillage']
            thisTask.bindData = {'pillage':{num:num,bind:[]}}
            thisTask.disRoom = [disRoom]
            thisTask.switch = false
            if(myRoom.mountMisson(thisTask))
            return colorful(`[carry]${roomName}挂载资源掠夺任务成功!`,'green',true)
            else
            return colorful(`[carry]${roomName}挂载资源掠夺任务失败!`,'red',true)
        },
        Cpillage(roomName:string,disRoom:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[build]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['CREEP']){
                if (i.Name == '资源掠夺' && i.disRoom[0] == disRoom)
                    myRoom.deleteMisson_ID(i.ID)
            }
            return colorful(`[build]${roomName}->${disRoom}资源掠夺任务 删除成功!`,'green',true)
        },
        /* 运输任务 */

    },

    /* 实验室相关 */
    olab:{
        /* 挂载合成任务相关 */
        compound(roomName:string,productName:ResourceConstant,Cindex:number[],Sindex1:number,Sindex2:number):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[lab]房间错误，请确认房间${roomName}！`
            var thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['lab合成'])
            /* 先查询合成数据表里是否有类似资源 */
            var mapData = LabMap[productName]
            if (!mapData) return `[lab]未找到资源${productName}的合成方法，请去常量文件中添加该数据!`
            var raw1 = mapData.raw1
            var raw2 = mapData.raw2
            thisTask.relateStructure = []
            thisTask.relateResource = [productName]
            thisTask.disResource = [raw1,raw2]
            /* 寻找所有相关旗帜 */
            var CflagList = [] as Flag[]
            for (var i of Cindex)
            {
                if (isInArray(CflagList,`${roomName}/${i}`) || !Game.flags[`${roomName}/${i}`])
                    return `[lab]出现重复旗帜或未找到旗帜!合成任务不能挂载!`
                    CflagList.push(Game.flags[`${roomName}/${i}`])
            }
            if (isInArray(CflagList,`${roomName}/${Sindex1}`) || isInArray(CflagList,`${roomName}/${Sindex2}`)|| !Game.flags[`${roomName}/${Sindex1}`]|| !Game.flags[`${roomName}/${Sindex2}`])
                return `[lab]出现重复旗帜或未找到旗帜!合成任务不能挂载!`
            var Sflag1 =Game.flags[`${roomName}/${Sindex1}`]
            var Sflag2 = Game.flags[`${roomName}/${Sindex2}`]
            /* 寻找旗帜下的lab  */
            var ClabList = []
            for (var ii of CflagList)
            {
                var tempLab = ii.pos.GetStructure('lab')
                if (!tempLab) return `[lab] 旗帜${ii.name}下未找到实验室建筑!合成任务不能挂载`
                ClabList.push(tempLab)
                thisTask.relateStructure.push(tempLab.id)
            }
            var Slab1 = Sflag1.pos.GetStructure('lab')
            var Slab2 = Sflag2.pos.GetStructure('lab')
            thisTask.disStructure = [Slab1.id,Slab2.id]
            if (!Slab1 || !Slab2) return `[lab] 旗帜${ii.name}下未找到实验室建筑!合成任务不能挂载`
            /* 给任务添加数据 */
            var Data = {}
            Data[Sflag1.name] = [Slab1.id,raw1,1]
            Data[Sflag2.name] = [Slab2.id,raw2,1]
            for (var f in CflagList)
            {
                Data[CflagList[f].name] = [ClabList[f].id,productName,1]
            }
            thisTask.LabBind = Data
            if(myRoom.mountMisson(thisTask))
            return `[upgrade]lab合成任务挂载成功! ${raw1}+${raw2}->${productName}`
            else
            return `[lab]lab合成任务挂载失败!`
        },
        Ccompound(roomName:string,productName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[lab]房间错误，请确认房间${roomName}！`
            for (var i of myRoom.memory.Task['STRUCTURE'])
            {
                if (i.Name == 'lab合成' && i.relateResource[0] == productName)
                    myRoom.deleteMisson_ID(i.ID)
            }
            return `[lab] 房间${roomName}已经清除所有lab合成${productName}的任务!`
        },
        /* 改变绑定信息的数量 未测试 */
        changeNum(roomName:string,flagIndex:number[],num:number):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[lab]房间错误，请确认房间${roomName}！`
            var structureIDs = []
            for (var i of flagIndex)
            {
                var flag = Game.flags[`${roomName}/${i}`]
                if (!flag) return `[lab]未找到对应旗帜!`
                var thisLab = flag.pos.GetStructure('lab')
                if (!thisLab) return `[lab]未找到对应实验室`
                structureIDs.push(thisLab.id)
            }
            for (var j of structureIDs)
            {
                if (!myRoom.memory.TaskData.OccupiedLab[j]) return `[lab]ID为${j},旗帜为${roomName}/${flagIndex[structureIDs.indexOf(j)]}的lab未被占用!`
            }
            for (var j of structureIDs)
            {
            myRoom.memory.TaskData.OccupiedLab[j].num = num
            }
            return `[lab]数量数据已修改!`
        },
        /* 展示房间内所有的lab绑定信息 未测试 */
        show(roomName:string):string{
            var myRoom = Game.rooms[roomName]
            if (!myRoom) return `[lab]房间错误，请确认房间${roomName}！`
            if (!myRoom.memory.TaskMemory.labs) return `[lab]暂时未发现房间内的lab建筑!`
            var message = `房间${roomName}的lab信息:\n`
            for (var i of myRoom.memory.TaskMemory.labs)
            {
                var flagMessage:string
                var lab = Game.getObjectById(i) as StructureLab
                var flag_ = lab.pos.lookFor(LOOK_FLAGS)[0]
                if (flag_) flagMessage = `${flag_.name}`
                var occData = myRoom.memory.TaskData.OccupiedLab[i]
                if (!occData) 
                    message += `建筑ID:${i},旗帜${flagMessage?flagMessage:'未找到旗帜'}  空闲中....\n`
                else
                {
                    var taskName = []
                    for (var m of occData.relate)
                    {
                        var rTask = myRoom.getMisson_ID(m)
                        if (rTask) taskName.push(rTask.Name)
                    }
                    message += `建筑ID:${i},旗帜${flagMessage?flagMessage:'未找到旗帜'}\n`
                    message += colorful(`  占用数量:${occData.num} 相关资源:${occData.rType} 相关任务:${taskName}\n`,'green',true)
                } 
            }
            return message
        },
    },


    /* 任务输出调试屏蔽 */
    ignore:{
        add(name:string):string{
            if (!isInArray(Memory.ignoreMissonName,name))
                Memory.ignoreMissonName.push(name)
            return `[ignore] 已经将任务${name}添加进输出调试的忽略名单里!`
        },
        remove(name:string):string{
            if (isInArray(Memory.ignoreMissonName,name))
                {
                    var index = Memory.ignoreMissonName.indexOf(name)
                    Memory.ignoreMissonName.splice(index,1)
                    return `[ignore] 已经将任务${name}删除出输出调试的忽略名单里!`
                }
            return `[ignore] 删除 ${name} 出调试输出忽略名单失败!`
            
        },
    },
}